//
//  BaseViewController.swift
//  EyeVisionForPatients
//
//  Created by PMC on 2020/2/7.
//  Copyright © 2020 tao Tao. All rights reserved.
//

import UIKit
import MJRefresh
import JXSegmentedView

class BaseViewController: UIViewController {
    lazy var emptyView: EmptyDataView = {
        let empty = UINib.loadViewFrom(EmptyDataView.self)
        empty.isHidden = true
        return empty
    }()
    
    
    func setNavClear(){
        if #available(iOS 15.0, *) {
            let appearance = UINavigationBarAppearance()
            appearance.configureWithOpaqueBackground()
            appearance.backgroundColor = .clear
            appearance.shadowColor = .clear
            navigationController?.navigationBar.standardAppearance = appearance
            navigationController?.navigationBar.scrollEdgeAppearance = navigationController?.navigationBar.standardAppearance
        }else{
            navigationController?.navigationBar.setBackgroundImage(UIImage(color: .clear), for: .default)
            
        }
    }
    func setNavWhite(){
        if #available(iOS 15.0, *) {
            let appearance = UINavigationBarAppearance()
            appearance.configureWithOpaqueBackground()
            appearance.backgroundColor = .white
            appearance.shadowColor = .clear
            navigationController?.navigationBar.standardAppearance = appearance
            navigationController?.navigationBar.scrollEdgeAppearance = navigationController?.navigationBar.standardAppearance
        }else{
            navigationController?.navigationBar.setBackgroundImage(UIImage(color: .white), for: .default)
            
        }
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.backgroundColor = UIColor.background
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        navigationController?.navigationBar.setBackgroundImage(UIImage(color: .white), for: .default)
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        HUDTool.hideActivity()
    }
    
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        
        makeUI()
        bindViewModel()
    }
    
    func makeUI() {
        view.addSubview(emptyView)
        emptyView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
    }
    
    func bindViewModel() {
        
    }
    
    func refreshHeader(action: Selector) -> MJRefreshNormalHeader {
        let header = MJRefreshNormalHeader(refreshingTarget: self, refreshingAction: action)
        header.isAutomaticallyChangeAlpha = true
        header.lastUpdatedTimeLabel?.isHidden = true
        header.stateLabel?.isHidden = true
        return header
    }
    
    func refreshFooter(action: Selector) -> MJRefreshAutoNormalFooter {
        let footer = MJRefreshAutoNormalFooter(refreshingTarget: self, refreshingAction: action)
        footer.isAutomaticallyChangeAlpha = true
        footer.ignoredScrollViewContentInsetBottom = bottomSafeH
        footer.setTitle("没有更多数据~", for: .noMoreData)
        return footer
    }
    
    func noMoreDataFooter(_ title: String) -> MJRefreshBackNormalFooter {
        let footer = MJRefreshBackNormalFooter()
        footer.state = .noMoreData
        footer.setTitle(title, for: .noMoreData)
        footer.stateLabel?.textColor = .textGray
        return footer
    }
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        print("page.show: \(type(of: self))")
    }
    
    deinit {
        print("page.deinit: \(type(of: self))")
    }
}

class CLBaseViewController: BaseViewController, JXSegmentedListContainerViewListDelegate {
    init() {
        super.init(nibName: nil, bundle: nil)
        edgesForExtendedLayout = .bottom
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func listView() -> UIView {
        return view
    }
}
